AudioDeviceRepresents an audio output device wrapper that owns an AudioContext and helps create and manage Channel and Master routing objects. Usually constructed when calling resolveDefaultAudioOutputDevice().
import { AudioDevice } from "@fluex/fluexgl-dsp";
// MediaDeviceInfo is typically retrieved from navigator.mediaDevices.enumerateDevices()
const deviceInfo = myMediaDeviceInfo;
const device = new AudioDevice(deviceInfo);
const channel = device.createChannel();
// Create an additional master bus and set it as active
const master = device.createMasterChannel();
device.setMasterChannel(master);
Constructs a new AudioDevice for the given output device and creates an internal AudioContext along with its default master channel.
new AudioDevice(deviceInfo: MediaDeviceInfo): AudioDevice;
deviceInfo: MediaDeviceInfo - The selected output device information (from enumerateDevices()).deviceInfo: MediaDeviceInfoThe device information this instance was constructed with. Set via the constructor.
id: stringA unique id for this AudioDevice instance. Automatically generated when constructing the device. Should NOT be changed.
timestamp: numberCreation timestamp (Date.now()) for this AudioDevice instance.
context: AudioContextThe AudioContext owned by this device. Used for creating channels and master busses.
masterChannel: MasterThe currently selected active Master channel for this device.
masterChannels: Master[]All created Master channels owned by this device via createMasterChannel(). Note that the default masterChannel created in the constructor is not automatically pushed into this array.
sampleRate: number (readonly)The sample rate of context, captured at construction time.
baseLatency: number (readonly)The base latency of context, captured at construction time.
outputLatency: number (readonly)The output latency of context, captured at construction time.
state: AudioContextState (readonly)The state of context (e.g. "running", "suspended"), captured at construction time.
currentTime: number (readonly)The currentTime of context, captured at construction time. Because this is read once during construction, it does not update as time passes — use getContext().currentTime for a live value.
maximumFrequency: number (readonly)Half of context.sampleRate (the Nyquist frequency), captured at construction time.
getMasterChannel(): MasterReturns the currently active Master channel.
No arguments
setMasterChannel(channel: Master): voidSets the active Master channel. This is typically one of the entries in masterChannels. Logs an error and does nothing if the given channel is already the active one.
channel: Master - The master channel to set as active.voidcreateMasterChannel(): MasterCreates a new Master channel using this device’s AudioContext, stores it in masterChannels, and returns it.
No arguments
getContext(): AudioContextReturns this device’s AudioContext.
No arguments
AudioContextcreateChannel(label?: string): ChannelCreates and returns a new Channel using this device’s AudioContext.
label?: string - Optional label for the new channel.This class does not emit custom events.
This class does not define public getters or setters. Note that sampleRate, baseLatency, outputLatency, state, currentTime and maximumFrequency are plain readonly properties rather than getters, and are only computed once at construction time.
const device = new AudioDevice(deviceInfo);
const a = device.createChannel();
const b = device.createChannel();
// Route a into b, and b into the active master
a.send(b);
b.send(device.getMasterChannel());
const device = new AudioDevice(deviceInfo);
const masterA = device.getMasterChannel();
const masterB = device.createMasterChannel();
// Switch active master
device.setMasterChannel(masterB);
// You can still keep references to older master channels
console.log(masterA.id, masterB.id);